package ar.edu.unlu.sistemas.p2p.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.ServiceConnection;
import android.os.Environment;
import android.os.IBinder;
import ar.edu.unlu.sistemas.p2p.business.util.Constants;

public abstract class AbsP2PActivity extends Activity{
	
	protected AppService appService;
	protected ServiceConnection mConnection = new ServiceConnection() {
		
	    public void onServiceConnected(ComponentName className, IBinder service) {
	
	        appService = ((AppService.LocalBinder)service).getService();
	        onServiceConnectedWithActivity();
	    }

	    public void onServiceDisconnected(ComponentName className) {
	
	        appService = null;
	        /*Toast.makeText(AbsP2PActivity.this, "Desconectado del servicio",
	                Toast.LENGTH_SHORT).show();*/
	    }
	};
	
	public void onServiceConnectedWithActivity() {
	};
	
	public void onBackPressed() {
		super.onBackPressed();
    	this.unbindService(this.mConnection);
    	this.finish();
    }
	
	public void showError(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg)
			   .setTitle("Error!")
		       .setCancelable(false)
		       .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                AbsP2PActivity.this.finish();
		           }
		       });
		       /*.setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });*/
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	protected int getSDCardState() {

		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			return Constants.SD_MOUNTED_WRITE;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			return Constants.SD_MOUNTED_READ;
		} else {
			// Something else is wrong. It may be one of many other states, but all we need
			//  to know is we can neither read nor write
			return Constants.SD_UNMOUNTED;
		}
	}

}
